// this class stores a collection of collections.
// it has 5 arraycollections, one for each of the five result types.
// An object of this class should be used to globally store the results
// returned by the Facebook search query.

package
{
	import App.as;
	
	import Event.as;
	
	import Group.as;
	
	import Page.as;
	
	import Person.as;
	
	import mx.collections.ArrayCollection;
	
	public class ResultCollection extends Result
	{
		public var personResults:ArrayCollection;
		public var groupResults:ArrayCollection;
		public var eventResults:ArrayCollection;
		public var pageResults:ArrayCollection;
		public var appResults:ArrayCollection;
		
		// Constructor
		public function ResultCollection()
		{
			personResults = new ArrayCollection();
			groupResults = new ArrayCollection();
			eventResults = new ArrayCollection();
			pageResults = new ArrayCollection();
			appResults = new ArrayCollection();
		}
		
		// Private helper method. Given a type and data,
		// stores the data into the appropriate arraycollection.
		private function addResultToAppropriateCollection(type:String, data:String){
			switch (type) {
				case "user":
					var aPerson:Person = new Person(data);
					personResults.addItem(aPerson);
					break ;
				case "group":
					var aGroup:Group = new Group(data);
					groupResults.addItem(aGroup);
					break ;
				case "event":
					var anEvent:Event = new Event(data);
					eventResults.addItem(anEvent);
					break ;
				case "application":
					var anApp:App = new App(data);
					appResults.addItem(anApp);
					break ;
				case "page":
					var aPage:Page = new Page(data);
					pageResults.addItem(aPage);
					break;
			} 
			
		}
		
		// This function assumes you know what type the object is.
		// pass the name of the type (user, group, etc.) and the json
		// object as a string.
		// The function will store the result object into the appropriate 
		// array collection.
		public function addResult(typeOfResult:String, jsonData:String){
				addResultToAppropriateCollection(typeOfResult, jsonData);		
		}
		
		// This function takes a json document string as input,
		// determines which kind of result it is (person, group, etc)
		// and puts it in the appropriate arraycollection.
		public function addResult(jsonData:String){
			var detectedType:String = findField("type",jsonData);
			addResultToAppropriateCollection(detectedType, jsonData);		
		}
		
	}
}